package cn.yunhe.dao.impl;

import cn.yunhe.dao.HouseDao;
import cn.yunhe.model.House;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.sql.SQLException;
import java.util.List;

public class HouseDaoImpl extends HibernateDaoSupport implements HouseDao {
    @Override
    public boolean insertHouse(House house) {
        getHibernateTemplate().save(house);
        return true;
    }

    @Override
    public boolean updateHouse(House house) {
        getHibernateTemplate().update(house);
        return true;
    }

    @Override
    public boolean deleteHouse(int hid) {
        getHibernateTemplate().delete(this.getHouseById(hid));
        return true;
    }

    @Override
    public House getHouseById(int hid) {
        House house= getHibernateTemplate().get(House.class,hid);
        return house;
    }

    @Override
    public List<House> searchHouse(final House house) {
        List<House>houseList=getHibernateTemplate().executeFind(new HibernateCallback<List<House>>() {
            @Override
            public List<House> doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria=session.createCriteria(House.class);
                if (house!=null){
                    if (house.getHtitle()!=null&&!house.getHtitle().equals("")){
                        criteria.add(Restrictions.like("htitle",house.getHtitle(),MatchMode.ANYWHERE));
                    }
                }
                List<House>houseList111=criteria.list();
                return houseList111;
            }
        });
        return houseList;
    }

    @Override
    public List<House> searchHouse(String hql) {
        List<House>houseList=getHibernateTemplate().find(hql);
        return houseList;
    }
}
